package ui;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import ui.factory.SingletonFactory;
import ui.panel.setup.subsidiary.ScreenSaverTimeStage;
import ui.util.Window;

import java.util.regex.Pattern;

public class Test1 extends Window {
    private static Test1 instance = null;
    double a;
    int k=5;
    int which=1;//改哪个框里的内容
    String str;
    //调用单例工厂
    public static Test1 getInstance() {
        if (instance == null) {
            instance = SingletonFactory.getWeakInstace(Test1.class);
        }
        return instance;
    }

    public Test1() {
        TextField textField1 = new TextField("");
        TextField textField2 = new TextField("");
        TextField textField3 = new TextField("");
        Text t1 = new Text("高度(m) = ");
        Text t2 = new Text("下落时间(s) = ");
        Text t3 = new Text("保留小数位(默认5) = ");

        //文本框里的灰色提示
        textField1.setPromptText("请输入高度");
        textField2.setPromptText("请输入时长");
        textField3.setPromptText("结果保留小数位数");
        //设置焦点
        textField1.setFocusTraversable(false);
        textField2.setFocusTraversable(false);
        textField3.setFocusTraversable(false);

        //网格布局
        GridPane gridPane = new GridPane();
        gridPane.add(t1, 0, 0);
        gridPane.add(textField1, 1, 0);
        gridPane.add(t2, 0, 1);
        gridPane.add(textField2, 1, 1);
        gridPane.add(t3, 0, 2);
        gridPane.add(textField3, 1, 2);
        //布局对齐
        gridPane.setAlignment(Pos.TOP_CENTER);

        //布局的内边距
        gridPane.setPadding(new Insets(10, 10, 10, 0));

        //场景
        Scene scene = new Scene(gridPane);

        //组件的外边距
        GridPane.setMargin(t1, new Insets(10));
        GridPane.setMargin(t2, new Insets(10));
        GridPane.setMargin(t3, new Insets(10));

        //调整组件对齐
        GridPane.setHalignment(t1, HPos.RIGHT);
        GridPane.setHalignment(t2, HPos.RIGHT);
        GridPane.setHalignment(t3, HPos.RIGHT);

        //添加监听
        //监听内容变化
        textField1.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if (textField1.isFocused()) {
                    //过滤出数字
                    str = Pattern.compile("[^0-9]").matcher(newValue).replaceAll("");
                    //啥都不填 a=0
                    if (str.equals("")) {
                        textField2.clear();
                        a = 0;
                    }
                    //正常情况
                    else {
                        a = Double.parseDouble(str);
                        textField2.setText(String.valueOf(String.format("%." + k + "f", Math.pow(2 * a / 9.8, 0.5))));
                    }
                    which = 2;
                }

            }
        });

        textField2.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if (textField2.isFocused()) {
                    str = Pattern.compile("[^0-9]").matcher(newValue).replaceAll("");
                    if (str.equals("")) {
                        textField1.clear();
                        a = 0;
                    } else {
                        a = Double.parseDouble(str);
                        textField1.setText(String.valueOf(String.format("%." + k + "f", 0.5 * 9.8 * Math.pow(a, 2))));
                    }
                    which = 1;
                }
            }
        });

        textField3.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                str = Pattern.compile("[^0-9]").matcher(newValue).replaceAll("");
                if (str.equals("")) {
                    k = 5;
                } else if (!textField2.getText().isEmpty()) {
                    k = Integer.parseInt(str);
                }
                if (which == 2)
                    textField2.setText(String.valueOf(String.format("%." + k + "f", Math.pow(2 * a / 9.8, 0.5))));
                else
                    textField1.setText(String.valueOf(String.format("%." + k + "f", Math.pow(2 * a / 9.8, 0.5))));
            }
        });

        setTitle("计算自由落体");
        setScene(scene);
        setHeight(200);
        setWidth(400);

    }
}

